const RongRTC = require('../../libs/RongRTC-wechat-minip-3.2.3');
const RongCallLib = require('../../libs/RongCallLib-wechat-minip-3.2.3');

const {
  ObserverList
} = require('../../pages/common.js');
const utils = require('../../libs/undescore-1.8.3.js');

const videoWatcher = new ObserverList();
const commandWatcher = new ObserverList();

module.exports = (config) => {
  let {
    im: RongIMLib,
    url,
  } = config;

  let rongCallLib = RongCallLib.init({
    timeout: 20000,
    RongIMLib: RongIMLib,
    RongRTC: RongRTC,
    url,
  });

  const watchVideo = (watcher) => {
    videoWatcher.add(watcher, true);
  };

  const watchCommand = (watcher) => {
    commandWatcher.add(watcher, true);
  };

  const destroy = () => {
    videoWatcher.remove();
    commandWatcher.remove();
    rongCallLib.destroy();
  }

  rongCallLib.videoWatch(function (result) {
    videoWatcher.notify(result);
  });
  rongCallLib.commandWatch(function (message) {
    commandWatcher.notify(message);
  });

  /* 
    params.conversationType,
    params.targetId,
  */
  const call = (params) => {
    const { conversationType, targetId, inviteUserIds, mediaType } = params;
    return new Promise((resolve, reject) => {
      rongCallLib.call({
        conversationType: conversationType,
        targetId: targetId,
        inviteUserIds: inviteUserIds,
        mediaType,
      }, (error) => {
        error ? reject(error) : resolve();
      });
    })
  };

  /* 
    params.conversationType,
    params.targetId,
  */
  const accept = (params) => {
    const { conversationType, targetId, inviteUserIds, mediaType } = params;
    return new Promise((resolve, reject) => {
      rongCallLib.accept({
        conversationType: conversationType,
        targetId: targetId,
        inviteUserIds: inviteUserIds,
        mediaType,
      }, function (error) {
        error ? reject(error) : resolve();
      });
    })
  };

  /* 
    params.conversationType,
    params.targetId,
  */
  const hungup = (params) => {
    const { conversationType, targetId } = params;
    return new Promise((resolve) => {
      rongCallLib.hungup({
        conversationType: conversationType,
        targetId: targetId
      }, function (error) {
        if (error) {
          console.warn('hungup', error);
        }
        resolve();
      });
    })
  };
  const invite = (params) => {
    const { conversationType, targetId, inviteUserIds, mediaType } = params;
    return new Promise((resolve, reject) => {
      rongCallLib.invite({
        conversationType,
        targetId,
        inviteUserIds,
        mediaType,
      }, function (error) {
        error ? reject(error) : resolve();
      });
    })
  };
  /* 
    params.conversationType,
    params.targetId,
  */
  const reject = (params) => {
    const { conversationType, targetId } = params;
    return new Promise((resolve, reject) => {
      rongCallLib.reject({
        conversationType: conversationType,
        targetId: targetId
      }, function (error) {
        error ? reject(error) : resolve();
      });
    })
  };


  return {
    call,
    accept,
    hungup,
    reject,
    invite,
    watchVideo,
    watchCommand,
    destroy,
    MessageType: RongCallLib.MessageType
  };
};